﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.Web.UI.HtmlControls;
using PanasonicWarranty.Business;
using System.Drawing;
using System.IO;
using Utils;
using System.Text;

namespace PanasonicWarranty.UI
{
    public partial class SmsStatistics : BasePageManageSms
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }
        
        private void BindDataSmsStatistic()
        {
            try
            {
                DateTime fromTime;
                DateTime toTime;
                if (!string.IsNullOrEmpty(txtFromDate.Text.Trim()))
                    fromTime = Convert.ToDateTime(txtFromDate.Text.Trim());
                else
                {
                    ShowMessageBox("Chưa chọn đầu thời điểm báo cáo");
                    return;
                }
                if (!string.IsNullOrEmpty(txtToDate.Text.Trim()))
                    toTime = Convert.ToDateTime(txtToDate.Text.Trim()).AddDays(1);
                else
                {
                    ShowMessageBox("Chưa chọn cuối thời điểm báo cáo");
                    return;
                }

                if (ddlStatisticType.SelectedValue == "1")
                {
                    grvData1.Visible = true;
                    grvData2.Visible = false;
                    grvData1.DataSource = SmsStatistic.StatisticSmsWarranty(fromTime, toTime);
                    grvData1.DataBind();
                }
                else
                {
                    grvData1.Visible = false;
                    grvData2.Visible = true;
                    grvData2.DataSource = SmsStatistic.StatisticMOMT(fromTime, toTime);
                    grvData2.DataBind();
                }
            }
            catch(Exception ex) {
                HandleException(ex);
            }   
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindDataSmsStatistic();
        }

        int sum1 = 0, sum2 = 0, sum3 = 0;
        protected void grvData_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    sum1 += int.Parse(e.Row.Cells[2].Text == "" ? "0" : e.Row.Cells[2].Text);
                    sum2 += int.Parse(e.Row.Cells[3].Text == "" ? "0" : e.Row.Cells[3].Text);
                    sum3 += int.Parse(e.Row.Cells[4].Text == "" ? "0" : e.Row.Cells[4].Text);
                }
                else if (e.Row.RowType == DataControlRowType.Footer)
                {
                    e.Row.Cells[1].Text = "Tổng";
                    e.Row.Cells[2].Text = sum1.ToString();
                    e.Row.Cells[3].Text = sum2.ToString();
                    e.Row.Cells[4].Text = sum3.ToString();
                }
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            try
            {
                Authorize(Role.Export);
                if (ddlStatisticType.SelectedValue == "1")
                {
                    ExportToExcel(grvData1, "thongketinnhanbaohanh.xls");
                }
                else
                {
                    ExportToExcel(grvData2, "thongkeluuluongtinnhan.xls");
                }
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }        
    }
}